package com.zhu.websocketdemo.service.impl;

import com.zhu.websocketdemo.pojo.PrivateMessage;
import com.zhu.websocketdemo.repository.PrivateMassageRepository;
import com.zhu.websocketdemo.service.IPrivateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PrivateMessageImpl implements IPrivateMessage {

    @Autowired
    private PrivateMassageRepository privateMassageRepository;
    @Override
    public PrivateMessage save(PrivateMessage privateMessage) {
        PrivateMessage save = privateMassageRepository.save(privateMessage);
        if (save != null) {
            return save;
        }
        throw new RuntimeException("信息保存失败");
    }

    @Override
    public List<PrivateMessage> findByFromUser(String fromUser) {
        List<PrivateMessage> byFromUser = privateMassageRepository.findByFromUser(fromUser);
        if (byFromUser != null){
            return byFromUser;
        }
        throw new RuntimeException("查询失败");
    }

    @Override
    public List<PrivateMessage> findByToUser(String toUser) {
        List<PrivateMessage> byToUser = privateMassageRepository.findByToUser(toUser);
        if (byToUser != null){
            return byToUser;
        }
        throw new RuntimeException("查询失败");
    }
}
